# -*- Python -*-

import os
import platform
import re
import subprocess

import lit.formats
import lit.util

# Configuration file for the 'lit' test runner.

# name: The name of this test suite.
config.name = 'eld unittests'

# All GoogleTests are named to have 'Tests' as their suffix. The '.' option is
# a special value for GoogleTest indicating that it should look through the
# entire testsuite recursively for tests (alternatively, one could provide a
# ;-separated list of subdirectories).
config.test_format = lit.formats.GoogleTest('.', 'Tests')

# suffixes: A list of file extensions to treat as test files.
config.suffixes = []

# test_exec_root: The root path where tests should be run.
eld_obj_root = getattr(config, 'eld_obj_root', None)
if eld_obj_root is not None:
    config.test_exec_root = os.path.join(eld_obj_root, 'test/Unittests/')

# Set llvm_{src,obj}_root for use by others.
config.llvm_src_root = getattr(config, 'llvm_src_root', None)
config.llvm_obj_root = getattr(config, 'llvm_obj_root', None)

# Tweak the PATH to include the tools dir and the scripts dir.
if eld_obj_root is not None:
    llvm_tools_dir = getattr(config, 'llvm_tools_dir', None)
    if not llvm_tools_dir:
        lit_config.fatal('No LLVM tools dir set!')
    path = os.path.pathsep.join((llvm_tools_dir, config.environment['PATH']))
    path = os.path.pathsep.join((os.path.join(getattr(config, 'llvm_src_root', None),'test','Scripts'),path))

    config.environment['PATH'] = path

    llvm_libs_dir = getattr(config, 'llvm_libs_dir', None)
    if not llvm_libs_dir:
        lit_config.fatal('No LLVM libs dir set!')
    path = os.path.pathsep.join((llvm_libs_dir,
                                 config.environment.get('LD_LIBRARY_PATH','')))
    config.environment['LD_LIBRARY_PATH'] = path

    # Propagate LLVM_SRC_ROOT into the environment.
    config.environment['LLVM_SRC_ROOT'] = getattr(config, 'llvm_src_root', '')

    # Propagate PYTHON_EXECUTABLE into the environment
    config.environment['PYTHON_EXECUTABLE'] = getattr(config, 'python_executable',
                                                      '')
# Shell execution
if platform.system() not in ['Windows'] or lit_config.getBashPath() != '':
    config.available_features.add('shell')
